from typing import Optional, Dict, Any

class MockI2C:
    """
    Mock I2C bus class
    Used for testing I2C communication without hardware
    """
    def __init__(self, scl: int, sda: int, freq: int = 100000) -> None:
        self.scl = scl
        self.sda = sda
        self.freq = freq
        self.devices: Dict[int, Dict[str, Any]] = {}
    def register_device(self, addr: int, data: bytes) -> None:
        self.devices[addr] = {'data': data, 'read_index': 0}
    def readfrom(self, addr: int, nbytes: int) -> bytes:
        if addr not in self.devices:
            raise OSError(f"Device at address 0x{addr:02X} not found")
        device = self.devices[addr]
        start = device['read_index']
        end = start + nbytes
        result = device['data'][start:end]
        device['read_index'] = end % len(device['data'])
        print(f"I2C read from 0x{addr:02X}: {result.hex()}")
        return result

def i2c_read_mock(i2c: MockI2C, addr: int, nbytes: int) -> Optional[bytes]:
    try:
        return i2c.readfrom(addr, nbytes)
    except Exception as e:
        print(f"I2C read error: {e}")
        return None
